package fr.orleans.team;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import entities.Player;
import entities.Team;
import entities.Tournament;
import fr.orleans.tools.JSFServices;
import fr.orleans.tools.ManagedBeans;

@ManagedBean
@ViewScoped
public class AddTeamToTournament extends ManagedBeans{

	private Team oneTeam;
	
	private List<Player> listPlayer;
	
	public AddTeamToTournament(){
		try{
			oneTeam = (Team) JSFServices.getParameterInRequestMap("team");
		}catch(Exception e){
			
		}
	}
	
	public Team getOneTeam() {
		return oneTeam;
	}

	public void setOneTeam(Team oneTeam) {
		this.oneTeam = oneTeam;
	}

	public List<Player> getListPlayer() {
		if(this.listPlayer == null){
			this.listPlayer = new ArrayList<Player>();
			
			for(int i = 0; i < 11; i ++){
				Player p = new Player();
				p.setTeam(this.getOneTeam());
				p.setNumber(i + 1);
				this.listPlayer.add(p);
			}
		}
		
		return listPlayer;
	}

	public void setListPlayer(List<Player> listPlayer) {
		this.listPlayer = listPlayer;
	}
	
	public String saveInformation(){
		this.oneTeam.setListPlayers(new HashSet<>(this.getListPlayer()));
		
		Tournament tournament = this.getOneTeam().getTournament();
		if(tournament.getListTeams() == null)
			tournament.setListTeams(new HashSet<Team>());
		
		tournament.getListTeams().add(this.getOneTeam());
		
		this.getService().getWebAction().editTournament(tournament);
		
		return "/modules/index/index.jsf";
	}
}